package tfmcbs.diagram.edit.parts;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.ResizableEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.FlowLayoutEditPolicy;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;

import tfmcbs.diagram.edit.policies.Artifact2ItemSemanticEditPolicy;
import tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry;
import tfmcbs.diagram.providers.ComponentBasedSystemElementTypes;

/**
 * @generated
 */
public class Artifact2EditPart extends ShapeNodeEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 3022;

	/**
	 * @generated
	 */
	protected IFigure contentPane;

	/**
	 * @generated
	 */
	protected IFigure primaryShape;

	/**
	 * @generated
	 */
	public Artifact2EditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new Artifact2ItemSemanticEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
		// XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
		// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
	}

	/**
	 * @generated
	 */
	protected LayoutEditPolicy createLayoutEditPolicy() {

		FlowLayoutEditPolicy lep = new FlowLayoutEditPolicy() {

			protected Command createAddCommand(EditPart child, EditPart after) {
				return null;
			}

			protected Command createMoveChildCommand(EditPart child,
					EditPart after) {
				return null;
			}

			protected Command getCreateCommand(CreateRequest request) {
				return null;
			}
		};
		return lep;
	}

	/**
	 * @generated
	 */
	protected IFigure createNodeShape() {
		return primaryShape = new ArtifactFigure();
	}

	/**
	 * @generated
	 */
	public ArtifactFigure getPrimaryShape() {
		return (ArtifactFigure) primaryShape;
	}

	/**
	 * @generated
	 */
	protected boolean addFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof ArtifactName2EditPart) {
			((ArtifactName2EditPart) childEditPart).setLabel(getPrimaryShape()
					.getFigureArtifactName());
			return true;
		}
		if (childEditPart instanceof ArtifactStereotype2EditPart) {
			((ArtifactStereotype2EditPart) childEditPart)
					.setLabel(getPrimaryShape().getFigureArtifactStereotype());
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected boolean removeFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof ArtifactName2EditPart) {
			return true;
		}
		if (childEditPart instanceof ArtifactStereotype2EditPart) {
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected void addChildVisual(EditPart childEditPart, int index) {
		if (addFixedChild(childEditPart)) {
			return;
		}
		super.addChildVisual(childEditPart, -1);
	}

	/**
	 * @generated
	 */
	protected void removeChildVisual(EditPart childEditPart) {
		if (removeFixedChild(childEditPart)) {
			return;
		}
		super.removeChildVisual(childEditPart);
	}

	/**
	 * @generated
	 */
	protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {
		return getContentPane();
	}

	/**
	 * @generated
	 */
	protected NodeFigure createNodePlate() {
		DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(40, 40);
		return result;
	}

	/**
	 * @generated
	 */
	public EditPolicy getPrimaryDragEditPolicy() {
		EditPolicy result = super.getPrimaryDragEditPolicy();
		if (result instanceof ResizableEditPolicy) {
			ResizableEditPolicy ep = (ResizableEditPolicy) result;
			ep.setResizeDirections(PositionConstants.WEST
					| PositionConstants.EAST);
		}
		return result;
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated
	 */
	protected NodeFigure createNodeFigure() {
		NodeFigure figure = createNodePlate();
		figure.setLayoutManager(new StackLayout());
		IFigure shape = createNodeShape();
		figure.add(shape);
		contentPane = setupContentPane(shape);
		return figure;
	}

	/**
	 * Default implementation treats passed figure as content pane.
	 * Respects layout one may have set for generated figure.
	 * @param nodeShape instance of generated figure class
	 * @generated
	 */
	protected IFigure setupContentPane(IFigure nodeShape) {
		if (nodeShape.getLayoutManager() == null) {
			ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
			layout.setSpacing(5);
			nodeShape.setLayoutManager(layout);
		}
		return nodeShape; // use nodeShape itself as contentPane
	}

	/**
	 * @generated
	 */
	public IFigure getContentPane() {
		if (contentPane != null) {
			return contentPane;
		}
		return super.getContentPane();
	}

	/**
	 * @generated
	 */
	protected void setForegroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setForegroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setBackgroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setBackgroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineWidth(int width) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineWidth(width);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineType(int style) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineStyle(style);
		}
	}

	/**
	 * @generated
	 */
	public EditPart getPrimaryChildEditPart() {
		return getChildBySemanticHint(ComponentBasedSystemVisualIDRegistry
				.getType(ArtifactName2EditPart.VISUAL_ID));
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMARelTypesOnSource() {
		ArrayList<IElementType> types = new ArrayList<IElementType>(3);
		types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		types.add(ComponentBasedSystemElementTypes.Manifestation_4039);
		types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMARelTypesOnSourceAndTarget(
			IGraphicalEditPart targetEditPart) {
		LinkedList<IElementType> types = new LinkedList<IElementType>();
		if (targetEditPart instanceof PrimitiveComponentEditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof CompositeComponentEditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof InterfaceEditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof ArtifactEditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof PrimitiveComponent2EditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof CompositeComponent2EditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof Interface2EditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof tfmcbs.diagram.edit.parts.Artifact2EditPart) {
			types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		}
		if (targetEditPart instanceof PrimitiveComponentEditPart) {
			types.add(ComponentBasedSystemElementTypes.Manifestation_4039);
		}
		if (targetEditPart instanceof CompositeComponentEditPart) {
			types.add(ComponentBasedSystemElementTypes.Manifestation_4039);
		}
		if (targetEditPart instanceof PrimitiveComponent2EditPart) {
			types.add(ComponentBasedSystemElementTypes.Manifestation_4039);
		}
		if (targetEditPart instanceof CompositeComponent2EditPart) {
			types.add(ComponentBasedSystemElementTypes.Manifestation_4039);
		}
		if (targetEditPart instanceof PrimitiveComponentEditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		if (targetEditPart instanceof CompositeComponentEditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		if (targetEditPart instanceof InterfaceEditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		if (targetEditPart instanceof ArtifactEditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		if (targetEditPart instanceof PrimitiveComponent2EditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		if (targetEditPart instanceof CompositeComponent2EditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		if (targetEditPart instanceof Interface2EditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		if (targetEditPart instanceof tfmcbs.diagram.edit.parts.Artifact2EditPart) {
			types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		}
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMATypesForTarget(IElementType relationshipType) {
		LinkedList<IElementType> types = new LinkedList<IElementType>();
		if (relationshipType == ComponentBasedSystemElementTypes.Dependency_4038) {
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_2022);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_2023);
			types.add(ComponentBasedSystemElementTypes.Interface_2024);
			types.add(ComponentBasedSystemElementTypes.Artifact_2025);
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_3019);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_3020);
			types.add(ComponentBasedSystemElementTypes.Interface_3021);
			types.add(ComponentBasedSystemElementTypes.Artifact_3022);
		} else if (relationshipType == ComponentBasedSystemElementTypes.Manifestation_4039) {
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_2022);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_2023);
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_3019);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_3020);
		} else if (relationshipType == ComponentBasedSystemElementTypes.EntityExtends_4046) {
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_2022);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_2023);
			types.add(ComponentBasedSystemElementTypes.Interface_2024);
			types.add(ComponentBasedSystemElementTypes.Artifact_2025);
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_3019);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_3020);
			types.add(ComponentBasedSystemElementTypes.Interface_3021);
			types.add(ComponentBasedSystemElementTypes.Artifact_3022);
		}
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMARelTypesOnTarget() {
		ArrayList<IElementType> types = new ArrayList<IElementType>(2);
		types.add(ComponentBasedSystemElementTypes.Dependency_4038);
		types.add(ComponentBasedSystemElementTypes.EntityExtends_4046);
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMATypesForSource(IElementType relationshipType) {
		LinkedList<IElementType> types = new LinkedList<IElementType>();
		if (relationshipType == ComponentBasedSystemElementTypes.Dependency_4038) {
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_2022);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_2023);
			types.add(ComponentBasedSystemElementTypes.Interface_2024);
			types.add(ComponentBasedSystemElementTypes.Artifact_2025);
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_3019);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_3020);
			types.add(ComponentBasedSystemElementTypes.Interface_3021);
			types.add(ComponentBasedSystemElementTypes.Artifact_3022);
		} else if (relationshipType == ComponentBasedSystemElementTypes.EntityExtends_4046) {
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_2022);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_2023);
			types.add(ComponentBasedSystemElementTypes.Interface_2024);
			types.add(ComponentBasedSystemElementTypes.Artifact_2025);
			types.add(ComponentBasedSystemElementTypes.PrimitiveComponent_3019);
			types.add(ComponentBasedSystemElementTypes.CompositeComponent_3020);
			types.add(ComponentBasedSystemElementTypes.Interface_3021);
			types.add(ComponentBasedSystemElementTypes.Artifact_3022);
		}
		return types;
	}

	/**
	 * @generated
	 */
	public class ArtifactFigure extends RoundedRectangle {

		/**
		 * @generated
		 */
		private WrappingLabel fFigureArtifactName;
		/**
		 * @generated
		 */
		private WrappingLabel fFigureArtifactStereotype;

		/**
		 * @generated
		 */
		public ArtifactFigure() {

			FlowLayout layoutThis = new FlowLayout();
			layoutThis.setStretchMinorAxis(false);
			layoutThis.setMinorAlignment(FlowLayout.ALIGN_CENTER);

			layoutThis.setMajorAlignment(FlowLayout.ALIGN_CENTER);
			layoutThis.setMajorSpacing(5);
			layoutThis.setMinorSpacing(5);
			layoutThis.setHorizontal(false);

			this.setLayoutManager(layoutThis);

			this.setCornerDimensions(new Dimension(getMapMode().DPtoLP(8),
					getMapMode().DPtoLP(8)));
			this.setForegroundColor(ColorConstants.black);
			this.setBackgroundColor(ColorConstants.lightGray);

			this.setBorder(new MarginBorder(getMapMode().DPtoLP(5),
					getMapMode().DPtoLP(5), getMapMode().DPtoLP(5),
					getMapMode().DPtoLP(5)));
			createContents();
		}

		/**
		 * @generated
		 */
		private void createContents() {

			fFigureArtifactStereotype = new WrappingLabel();
			fFigureArtifactStereotype.setText("unknown");

			fFigureArtifactStereotype.setFont(FFIGUREARTIFACTSTEREOTYPE_FONT);

			this.add(fFigureArtifactStereotype);

			fFigureArtifactName = new WrappingLabel();
			fFigureArtifactName.setText("<...>");

			fFigureArtifactName.setFont(FFIGUREARTIFACTNAME_FONT);

			this.add(fFigureArtifactName);

		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureArtifactName() {
			return fFigureArtifactName;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureArtifactStereotype() {
			return fFigureArtifactStereotype;
		}

	}

	/**
	 * @generated
	 */
	static final Font FFIGUREARTIFACTSTEREOTYPE_FONT = new Font(
			Display.getCurrent(), Display.getDefault().getSystemFont()
					.getFontData()[0].getName(), 9, SWT.ITALIC);

	/**
	 * @generated
	 */
	static final Font FFIGUREARTIFACTNAME_FONT = new Font(Display.getCurrent(),
			Display.getDefault().getSystemFont().getFontData()[0].getName(), 9,
			SWT.BOLD);

}
